package com.raidpoints.client;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Button;

public class AddCharacterBox extends DialogBox {
	AccountPage ap;
	ArrayList<gameinfo> games;
	ListBox roleBox,classBox,gameBox,serverBox,sroleBox;
	character mc;
	Button addButton;
	private Label nameLabel;

	public AddCharacterBox(AccountPage map){
		this(map,null,null);
	}

	/**
	 * @wbp.parser.constructor
	 */
	public AddCharacterBox(AccountPage map,character tc,final RaidPoints rp) {
		setHTML("Add Character To Account");
		ap = map;
		mc = tc;

		//		if(mc != null){
		//			if(mc.gid == 1) mc.game="World of Warcraft";
		//			else if(mc.gid == 2) mc.game="SWTOR";
		//		}

		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("247px", "360px");

		nameLabel = new Label("Name:");
		layoutPanel.add(nameLabel);
		layoutPanel.setWidgetLeftWidth(nameLabel, 18.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(nameLabel, 45.0, Unit.PX, 16.0, Unit.PX);

		final TextBox nameBox = new TextBox();
		layoutPanel.add(nameBox);
		layoutPanel.setWidgetLeftWidth(nameBox, 18.0, Unit.PX, 202.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(nameBox, 67.0, Unit.PX, 32.0, Unit.PX);

		Label lblNewLabel = new Label("Game:");
		layoutPanel.add(lblNewLabel);
		layoutPanel.setWidgetLeftWidth(lblNewLabel, 18.0, Unit.PX, 47.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel, 23.0, Unit.PX, 16.0, Unit.PX);

		gameBox = new ListBox();
		layoutPanel.add(gameBox);
		layoutPanel.setWidgetLeftWidth(gameBox, 80.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(gameBox, 15.0, Unit.PX, 24.0, Unit.PX);

		Label lblServer = new Label("Server:");
		layoutPanel.add(lblServer);
		layoutPanel.setWidgetLeftWidth(lblServer, 18.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblServer, 113.0, Unit.PX, 16.0, Unit.PX);

		serverBox = new ListBox();
		layoutPanel.add(serverBox);
		layoutPanel.setWidgetLeftWidth(serverBox, 80.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(serverBox, 105.0, Unit.PX, 24.0, Unit.PX);

		Label lblClass = new Label("Class:");
		layoutPanel.add(lblClass);
		layoutPanel.setWidgetLeftWidth(lblClass, 18.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblClass, 156.0, Unit.PX, 16.0, Unit.PX);

		classBox = new ListBox();
		layoutPanel.add(classBox);
		layoutPanel.setWidgetLeftWidth(classBox, 80.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(classBox, 148.0, Unit.PX, 24.0, Unit.PX);

		Label lblRole = new Label("Prim. Role:");
		layoutPanel.add(lblRole);
		layoutPanel.setWidgetLeftWidth(lblRole, 16.0, Unit.PX, 69.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblRole, 198.0, Unit.PX, 16.0, Unit.PX);

		roleBox = new ListBox();
		layoutPanel.add(roleBox);
		layoutPanel.setWidgetLeftWidth(roleBox, 80.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(roleBox, 190.0, Unit.PX, 24.0, Unit.PX);

		addButton = new Button("Add");
		layoutPanel.add(addButton);
		layoutPanel.setWidgetLeftWidth(addButton, 25.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(addButton, 321.0, Unit.PX, 28.0, Unit.PX);

		Button cancelButton = new Button("Cancel");
		layoutPanel.add(cancelButton);
		layoutPanel.setWidgetLeftWidth(cancelButton, 122.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(cancelButton, 321.0, Unit.PX, 28.0, Unit.PX);

		Label lblSecRole = new Label("Sec. Roles:");
		layoutPanel.add(lblSecRole);
		layoutPanel.setWidgetLeftWidth(lblSecRole, 16.0, Unit.PX, 69.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblSecRole, 224.0, Unit.PX, 16.0, Unit.PX);

		sroleBox = new ListBox();
		sroleBox.setMultipleSelect(true);
		sroleBox.setWidth("140px");
		layoutPanel.add(sroleBox);
		layoutPanel.setWidgetLeftWidth(sroleBox, 80.0, Unit.PX, 140.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(sroleBox, 220.0, Unit.PX, 95.0, Unit.PX);

		gameBox.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				UpdateBoxes();
			}
		});

		if(mc != null){
			nameBox.setText(mc.name);
			if(rp.auth != RaidPoints.AuthType.admin) nameBox.setEnabled(false);
			serverBox.setEnabled(false);
			gameBox.setEnabled(false);
		}

		cancelButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});

		final AsyncCallback<String> addCallback = new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Char add failed");
				hide();
			}

			@Override
			public void onSuccess(String result) {
				if(result == null) {Window.alert("Char add failed"); hide(); return;}
				if(result.equals("nonunique")){
					Window.alert("Character already added to an account");
					hide();
					return;
				}
				if(ap != null) ap.Refresh();
				hide();
			}

		};

		addButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				character tc = (mc == null) ? new character() : mc;
				int ngsid = games.get(gameBox.getSelectedIndex()).ngsid;
				if(ngsid == 0 && !nameBox.getText().matches("[a-zA-Z0-9]+")){
					Window.alert("Character name must be alphanumeric");
					return;
				}
				if(ngsid == 0 && (gameBox.getSelectedIndex() < 0 || serverBox.getSelectedIndex() < 0 || roleBox.getSelectedIndex() < 0 || classBox.getSelectedIndex() < 0)){
					Window.alert("Must select all fields");
					return;
				}
				tc.name = nameBox.getText();
				tc.game = gameBox.getItemText(gameBox.getSelectedIndex());
				tc.ngsid = ngsid;
				if(ngsid == 0){
					tc.Class = classBox.getItemText(classBox.getSelectedIndex());
					tc.prole = roleBox.getItemText(roleBox.getSelectedIndex());
					if(tc.srole != null) tc.srole.clear();
					else tc.srole = new ArrayList<String>();
					for(int i = 0; i < sroleBox.getItemCount(); i++){
						if(sroleBox.isItemSelected(i)){
							tc.srole.add(sroleBox.getItemText(i));
						}
					}
					//tc.srole = sroleBox.getItemText(sroleBox.getSelectedIndex());
					tc.server = serverBox.getItemText(serverBox.getSelectedIndex());
				}
				if(mc == null) ap.rp.rpsvc.ChangeAccountChar(tc, addCallback);
				else if (ap ==null) {rp.rpsvc.UpdateAccountInfo(tc, addCallback);}
				else ap.rp.rpsvc.UpdateAccountInfo(tc, addCallback);
			}
		});


		AsyncCallback<ArrayList<gameinfo>> ginfoCallback = new AsyncCallback<ArrayList<gameinfo>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(ArrayList<gameinfo> result) {
				if(result == null) return;
				games = result;
				for(gameinfo g: games){
					gameBox.addItem(g.game);
					if(mc != null){
						if(g.game.equals(mc.game) || g.gid == mc.gid) gameBox.setSelectedIndex(gameBox.getItemCount() -1);
					}
				}

//				if(mc != null){
//					for(int i = 0; i < gameBox.getItemCount(); i++){
//						if(gameBox.getItemText(i).equals(mc.game)){
//							gameBox.setSelectedIndex(i);
//							break;
//						}
//					}
//				}
				UpdateBoxes();
			}

		};

		if(ap != null) ap.rp.rpsvc.GetAddInfo(ginfoCallback);
		else rp.rpsvc.GetAddInfo(ginfoCallback);

	}

	void UpdateBoxes(){
		if(games == null) return;
		gameinfo tg = null;
		for(gameinfo g: games){
			if(g.game.equals(gameBox.getItemText(gameBox.getSelectedIndex()))) {tg = g; break;}
		}
		if(tg == null) return;
		serverBox.clear(); roleBox.clear(); classBox.clear(); sroleBox.clear();

		if(tg.ngsid > 0){
			nameLabel.setText(tg.game + ":");
			serverBox.setEnabled(false); classBox.setEnabled(false); roleBox.setEnabled(false); sroleBox.setEnabled(false);
		}
		else{
			nameLabel.setText("Name:");
			serverBox.setEnabled(true); classBox.setEnabled(true); roleBox.setEnabled(true); sroleBox.setEnabled(true);
			//sroleBox.addItem("none"); sroleBox.setSelectedIndex(0);
			if(tg.servers != null) for(String s : tg.servers) serverBox.addItem(s);
					if(tg.classes != null) for(String s : tg.classes) classBox.addItem(s);
							if(tg.roles != null) for(String s : tg.roles) {roleBox.addItem(s); sroleBox.addItem(s);}
		}
		if(mc != null){
			if(mc.server != null) for(int i = 0; i < serverBox.getItemCount(); i++) if(serverBox.getItemText(i).equals(mc.server)) {serverBox.setSelectedIndex(i); break;}
			if(mc.Class != null) for(int i = 0; i < classBox.getItemCount(); i++) if(classBox.getItemText(i).equals(mc.Class)) {classBox.setSelectedIndex(i); break;}
			if(mc.prole != null) for(int i = 0; i < roleBox.getItemCount(); i++) if(roleBox.getItemText(i).equals(mc.prole)) {roleBox.setSelectedIndex(i); break;}
			if(mc.srole != null) for(int i = 0; i < sroleBox.getItemCount(); i++) if(mc.srole.contains(sroleBox.getItemText(i))) {sroleBox.setItemSelected(i, true);}
			addButton.setText("Update");
			setHTML("Update Character");			
		}
	}
}
